package example.org;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

public class ShareVia extends Activity implements OnClickListener {
	private Button mButtonUpload;
	private String lemparan;
	private Uri imageFileUri;
	private ImageView chosenImageView;
	private List<CheckBox> listShareVIA;
	private CheckBox checkFacebook, checkTwitter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sharevia);
		mButtonUpload = (Button) findViewById(R.id.button1);
		mButtonUpload.setOnClickListener(this);

		listShareVIA = new ArrayList<CheckBox>();

		checkFacebook = (CheckBox) findViewById(R.id.CheckFacebook);
		listShareVIA.add(checkFacebook);

		checkTwitter = (CheckBox) findViewById(R.id.CheckTwitter);
		listShareVIA.add(checkTwitter);

		chosenImageView = (ImageView) findViewById(R.id.imageView1);
		Bundle extra = getIntent().getExtras();
		if (extra != null) {
			lemparan = extra.getString("gambar");
		}

		// Display currentDisplay = getWindowManager().getDefaultDisplay();
		// int dw = currentDisplay.getWidth();
		// int dh = currentDisplay.getHeight() / 2 - 100;
		
		try {
			imageFileUri = Uri.parse(lemparan);
			BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
			bmpFactoryOptions.inJustDecodeBounds = true;
			Bitmap bmp = BitmapFactory.decodeStream(getContentResolver()
					.openInputStream(imageFileUri), null, bmpFactoryOptions);
			bmpFactoryOptions.inSampleSize = 2;
			bmpFactoryOptions.inJustDecodeBounds = false;
			bmp = BitmapFactory.decodeStream(getContentResolver()
					.openInputStream(imageFileUri), null, bmpFactoryOptions);
			chosenImageView.setImageBitmap(bmp);
		} catch (FileNotFoundException e) {
			Log.v("ERROR", e.toString());
		}
	}

	@Override
	public void onClick(View v) {
		boolean share = false;

		if (v == mButtonUpload) {
			for (CheckBox checkBox : listShareVIA) {
				if (checkBox.isChecked()) {
					share = true;
					break;
				}
			}

			if (share) {
				for (CheckBox checkBox : listShareVIA) {
					if (checkBox.isChecked()) {
						Toast.makeText(this,
								"Telah di Share VIA " + checkBox.getText(),
								Toast.LENGTH_SHORT).show();
					}
				}

				this.finish();
			} else {
				Toast.makeText(this, "Pilih Media", Toast.LENGTH_LONG).show();
			}
		}
	}
}
